package com.huyaanalysisplatform.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huyaanalysisplatform.entity.TBlueRay;
import com.huyaanalysisplatform.entity.TLiveCount;
import com.huyaanalysisplatform.entity.TLolHotHero;
import com.huyaanalysisplatform.mapper.TLiveCountMapper;
import com.huyaanalysisplatform.service.TLiveCountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Huadi Group 12
 * @since 2021-08-01
 */
@Service
public class TLiveCountServiceImpl extends ServiceImpl<TLiveCountMapper, TLiveCount> implements TLiveCountService {

    @Autowired
    TLiveCountMapper tLiveCountMapper;

    public Map queryAllArea(Map map) {
        System.out.println(new Date(121, 7, 8, 0, 0, 0));//查看起点时间点
        QueryWrapper<TLiveCount> queryWrapper1 = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "网游竞技");
        QueryWrapper<TLiveCount> queryWrapper2 = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "单机热游");
        QueryWrapper<TLiveCount> queryWrapper3 = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "娱乐天地");
        QueryWrapper<TLiveCount> queryWrapper4 = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "手游休闲");
        List<TLiveCount> tLiveCounts1 = this.list(queryWrapper1);
        List<TLiveCount> tLiveCounts2 = this.list(queryWrapper2);
        List<TLiveCount> tLiveCounts3 = this.list(queryWrapper3);
        List<TLiveCount> tLiveCounts4 = this.list(queryWrapper4);
        List<Map> anchorCountList = new ArrayList<>();
        for (int i = 0; i < tLiveCounts1.size(); i++) {
            Map liveCountsMap = new HashMap();
            liveCountsMap.put("time", i );
            liveCountsMap.put("netGame", tLiveCounts1.get(i).getLiveCount());
            liveCountsMap.put("singleGame", tLiveCounts2.get(i).getLiveCount());
            liveCountsMap.put("phoneGame", tLiveCounts3.get(i).getLiveCount());
            liveCountsMap.put("funGame", tLiveCounts4.get(i).getLiveCount());
            anchorCountList.add(liveCountsMap);
        }
        map.put("anchorCount", anchorCountList);

        return map;
    }

    public Map queryAll(Map map) {
        QueryWrapper<TLiveCount> queryWrapper1 = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "网游竞技");
        QueryWrapper<TLiveCount> queryWrapper2 = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "单机热游");
        QueryWrapper<TLiveCount> queryWrapper3 = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "娱乐天地");
        QueryWrapper<TLiveCount> queryWrapper4 = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "手游休闲");
        List<TLiveCount> tLiveCounts1 = this.list(queryWrapper1);
        List<TLiveCount> tLiveCounts2 = this.list(queryWrapper2);
        List<TLiveCount> tLiveCounts3 = this.list(queryWrapper3);
        List<TLiveCount> tLiveCounts4 = this.list(queryWrapper4);
        List<Map> anchorList = new ArrayList<>();
        for (int i = 0; i < tLiveCounts1.size(); i++) {
            Map liveCountsMap = new HashMap();
            int total = tLiveCounts1.get(i).getLiveCount() + tLiveCounts2.get(i).getLiveCount() + tLiveCounts3.get(i).getLiveCount() + tLiveCounts4.get(i).getLiveCount();
            liveCountsMap.put("time", i );
            liveCountsMap.put("count", total);
            anchorList.add(liveCountsMap);
        }
        map.put("anchor", anchorList);

        return map;
    }

    public Map queryLiveCount() {
        QueryWrapper<TLiveCount> queryWrapper = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .gt("date", new Date(121, 7, 8, 0, 0, 0));
        List<TLiveCount> tableData = this.list(queryWrapper);
        Map map = new HashMap();
        map.put("tableData", tableData);
        return map;
    }

    public Map addLiveCount(TLiveCount tLiveCount) {
        Map map = new HashMap();
        if (this.save(tLiveCount)) {
            map.put("code", 0);
            map.put("message", "成功");
            return map;
        } else {
            map.put("code", 1);
            map.put("message", "添加失败");
            return map;
        }
    }

    public Map editLiveCount(TLiveCount tLiveCount) {
        Map map = new HashMap();

        if (this.updateById(tLiveCount)) {
            map.put("code", 0);
            map.put("message", "成功");
            return map;
        } else {
            map.put("code", 1);
            map.put("message", "添加失败");
            return map;
        }
    }

    public Map deleteLiveCount(String id) {
        if (this.removeById(id)) {
            Map map = new HashMap();
            map.put("code", 0);
            map.put("message", "成功");
            return map;
        } else {
            Map map = new HashMap();
            map.put("code", 1);
            map.put("message", "添加失败");
            return map;
        }
    }

    public Map queryFirst() {
        Map map = new HashMap();
        QueryWrapper<TLiveCount> queryWrapper = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "网游竞技");
        List<TLiveCount> tLiveCounts = this.list(queryWrapper);
        List<Map> anchorList = new ArrayList<>();
        for (int i = 0; i < tLiveCounts.size(); i++) {
            Map liveCountsMap = new HashMap();
            int total = tLiveCounts.get(i).getLiveCount();
            liveCountsMap.put("time", i );
            liveCountsMap.put("count", total);
            anchorList.add(liveCountsMap);
        }
        map.put("anchor", anchorList);
        return map;
    }

    public Map querySecond() {
        Map map = new HashMap();
        QueryWrapper<TLiveCount> queryWrapper = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "单机热游");
        List<TLiveCount> tLiveCounts = this.list(queryWrapper);
        List<Map> anchorList = new ArrayList<>();
        for (int i = 0; i < tLiveCounts.size(); i++) {
            Map liveCountsMap = new HashMap();
            int total = tLiveCounts.get(i).getLiveCount();
            liveCountsMap.put("time", i );
            liveCountsMap.put("count", total);
            anchorList.add(liveCountsMap);
        }
        map.put("anchor", anchorList);
        return map;
    }

    public Map queryThird() {
        Map map = new HashMap();
        QueryWrapper<TLiveCount> queryWrapper = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "娱乐天地");
        List<TLiveCount> tLiveCounts = this.list(queryWrapper);
        List<Map> anchorList = new ArrayList<>();
        for (int i = 0; i < tLiveCounts.size(); i++) {
            Map liveCountsMap = new HashMap();
            int total = tLiveCounts.get(i).getLiveCount();
            liveCountsMap.put("time", i );
            liveCountsMap.put("count", total);
            anchorList.add(liveCountsMap);
        }
        map.put("anchor", anchorList);
        return map;
    }

    public Map queryFourth() {
        Map map = new HashMap();
        QueryWrapper<TLiveCount> queryWrapper = new QueryWrapper<TLiveCount>()
                .orderByAsc("date")
                .between("date", new Date(121, 7, 8, 0, 0, 0), new Date(121, 7, 8, 23, 59, 59))
                .eq("areaName", "手游休闲");
        List<TLiveCount> tLiveCounts = this.list(queryWrapper);
        List<Map> anchorList = new ArrayList<>();
        for (int i = 0; i < tLiveCounts.size(); i++) {
            Map liveCountsMap = new HashMap();
            int total = tLiveCounts.get(i).getLiveCount();
            liveCountsMap.put("time", i );
            liveCountsMap.put("count", total);
            anchorList.add(liveCountsMap);
        }
        map.put("anchor", anchorList);
        return map;
    }

    @Override
    public Map<String, List<Map<String, Object>>> rate() {
        List<TLiveCount> tLiveCounts = tLiveCountMapper.selectOneDay();
        List<Integer> countList = new ArrayList<>();
        List<TLiveCount> tempList = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<>();
        for (TLiveCount tLiveCount : tLiveCounts) {
            tempList.add(tLiveCount);
            if (tempList.size() == 4) {
                int totalLiveCount = 0;
                for (TLiveCount tLiveCount1 : tempList) {
                    totalLiveCount = totalLiveCount + tLiveCount1.getLiveCount();
                }
                countList.add(totalLiveCount);
                tempList.clear();
            }
        }
        for (int i = 0; i < countList.size() - 1; ++i) {
            Map<String, Object> liveCountMap = new HashMap<>();
            liveCountMap.put("time", i + 1);
            liveCountMap.put("count1", countList.get(i));
            liveCountMap.put("count2", countList.get(i + 1));
            double temp = (countList.get(i) - countList.get(i + 1)) / 1000.0 / countList.get(i + 1) * 1000;
            BigDecimal b = new BigDecimal(temp);
            double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            DecimalFormat df = new DecimalFormat("#.00");
//            String h = df.format(d);
            int s = new Double(f1 * 100).intValue();
            liveCountMap.put("rate", s);
            list.add(liveCountMap);
        }
        Map<String, List<Map<String, Object>>> map = new HashMap<>();
        map.put("tableData", list);
        return map;
    }
}
